package com.wang.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 流工具类
 */
@Slf4j
public class StreamUtil {

    /**
     * 关闭输入流
     * @param inputStreams 输入流
     */
    public static void closeInputStream(InputStream... inputStreams) {
        if (inputStreams == null || inputStreams.length < 1) return;
        for (InputStream inputStream : inputStreams) {
            if (inputStream == null) continue;
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("close input stream error: ", e);
            }
        }
    }

    /**
     * 关闭输出流
     * @param outputStreams 输出流
     */
    public static void closeOutputStream(OutputStream... outputStreams) {
        if (outputStreams == null || outputStreams.length < 1) return;
        for (OutputStream outputStream : outputStreams) {
            if (outputStream == null) continue;
            try {
                outputStream.close();
            } catch (IOException e) {
                log.error("close output stream error: ", e);
            }
        }
    }

    /**
     * 删除临时文件
     * @param tempFilePath 临时文件路径
     */
    public static void deleteLFile(String tempFilePath) {
        if (tempFilePath == null) return;
        File file = new File(tempFilePath);
        if (!file.exists() || !file.isFile()) return;
        if (!file.delete()) log.error("删除zip文件" + tempFilePath + "失败");
    }
}
